package cn.itsource.ymcc.consumer;

import cn.itsource.ymcc.constant.Constants;
import cn.itsource.ymcc.constant.CourseConstants;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.service.IPayOrderService;
import cn.itsource.ymcc.vo.PayOrderVo;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;

@Slf4j
@Component
//consumerGroup = 定义组
//topic = 主题
//selectorExpression = tag
@RocketMQMessageListener(
        consumerGroup = "PayConsumer",
        topic = CourseConstants.MQ_TOPIC_ORDER,
        selectorExpression = CourseConstants.MQ_TAGS_COURSEORDER_PAYORDER
)
public class PayConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IPayOrderService payOrderService;

    @Override
    public void onMessage(MessageExt messageExt) {
        try {
            //得到消息
            byte[] body = messageExt.getBody();
            //转为字符串  -- json字符串
            String s = new String(body, Constants.UTF8);
            PayOrderVo payOrderVo = JSON.parseObject(s,PayOrderVo.class);
            payOrderService.save(payOrderVo);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
